function submitAddRoleRequest(roleName) {
    let role = {
        "roleName": roleName
    }
    return request({
        url: baseUrl + "/admin/api/auth/role",
        method: 'post',
        data: role,
    });
}
function submitAddAuthorityRequest(authorityName){
    let authority = {
        "authorityName": authorityName
    }
    return request({
        url: baseUrl + "/admin/api/auth/authority",
        method: 'post',
        data: authority,
    });
}
function disPlayToUI(response) {
    let dbRole = response.data;
    let roleList = $("#roleList");
    let roleTr = `<tr>
            <td>${dbRole.roleName}</td>
            <td>2022-3-15</td>
            <td>geticsen</td>
            <td>2022-3-15</td>
            <td>geticsen</td>
            <td>
            <button onclick="modifyRole(1,this)" class="btn btn-icon btn-xs btn-warning">
                <i class="ion-edit"></i> 修改 
            </button>
            <button onclick="deleteRole(1,this)" class="btn btn-icon btn-xs btn-danger">
                <i class="ion-close-circled"></i> 删除 
            </button>
            </td>
        </tr>`;
    roleList.append(roleTr);
}

function disPlayToAuthorityUI(response) {
    let dbRole = response.data;
    let roleList = $("#authorityList");
    let roleTr = `<tr>
            <td>${dbRole.authorityName}</td>
            <td>2022-3-15</td>
            <td>geticsen</td>
            <td>2022-3-15</td>
            <td>geticsen</td>
            <td>
            <button onclick="modifyAuthority(dbRole.Id,this)" class="btn btn-icon btn-xs btn-warning">
                <i class="ion-edit"></i> 修改 
            </button>
            <button onclick="deleteAuthority(dbRole.Id,this)" class="btn btn-icon btn-xs btn-danger">
                <i class="ion-close-circled"></i> 删除 
            </button>
            </td>
        </tr>`;
    roleList.append(roleTr);
}

function addRole() {
    $.NZ_MsgBox.prompt({
        title: "输入角色名称",
        type: "warning",
        callback: function (res) {
            if (res !== null) {
                submitAddRoleRequest(res).then((response) => {
                    $.NZ_MsgBox.alert({
                        title: "成功！",
                        content: "添加角色成功",
                        type: "success"
                    });
                    disPlayToUI(response);
                    console.log(response)
                }).catch(function (error) {
                    console.log(error);
                });
                console.log(res)

            }
        }
    });
}

function modifyRole(roleId, object) {
    $.NZ_MsgBox.prompt({
        title: "修改角色名称",
        type: "warning",
        callback: function (res) {
            if (res !== null) {
                submitModifyRoleRequest(roleId, res).then((response) => {
                    $.NZ_MsgBox.alert({
                        title: "成功！",
                        content: "修改角色成功",
                        type: "success"
                    });
                    console.log(response)
                    $($($(object).parent().parent()).find("td")[0]).text(res)
                }).catch(function (error) {
                    console.log(error);
                });
                console.log(res)

            }
        }
    });

}

function submitModifyRoleRequest(id, roleName) {
    let role = {
        "roleName": roleName,
        "id": id
    }
    return request({
        url: baseUrl + "/admin/api/auth/role",
        method: 'put',
        data: role,
    });
}

function submitModifyAuthorityRequest(id,authorityName){
    let authority = {
        "authorityName": authorityName,
        "id": id
    }
    return request({
        url: baseUrl + "/admin/api/auth/authority",
        method: 'put',
        data: authority,
    });
}
function submitDeleteAuthorityRequest(authorityId, object) {
    request({
        url: baseUrl + "/admin/api/auth/authority/" + authorityId,
        method: 'delete',
    }).then((response) => {
        $.NZ_MsgBox.alert({
            title: "成功！",
            content: "删除权限成功！",
            type: "success"
        });
        $(object).parent().parent().remove();
    }).catch(function (error) {
        console.log(error);
    });
}
function submitDeleteRoleRequest(roleId, object) {
    request({
        url: baseUrl + "/admin/api/auth/role/" + roleId,
        method: 'delete',
    }).then((response) => {
        $.NZ_MsgBox.alert({
            title: "成功！",
            content: "删除角色成功！",
            type: "success"
        });
        $(object).parent().parent().remove();
    }).catch(function (error) {
        console.log(error);
    });
}

function deleteRole(roleId, object) {
    $.NZ_MsgBox.confirm({
        title: "删除"
        , content: "确认删除？"
        , type: "warning"
        , callback: function (confirm) {
            if (confirm) {
                submitDeleteRoleRequest(roleId, object);
            }
        }
    });

}

function addAuthority() {
    // let roleList = $("#authorityList");
    $.NZ_MsgBox.prompt({
        title: "输入权限名称",
        type: "warning",
        callback: function (res) {
            if (res !== null) {
                submitAddAuthorityRequest(res).then((response) => {
                    $.NZ_MsgBox.alert({
                        title: "成功！",
                        content: "添加权限成功",
                        type: "success"
                    });
                    disPlayToAuthorityUI(response);
                    console.log(response)
                }).catch(function (error) {
                    console.log(error);
                });
                console.log(res)
            }
        }
    });
}

function modifyAuthority(authorityId, object) {
    $.NZ_MsgBox.prompt({
        title: "修改权限名称",
        type: "warning",
        callback: function (res) {
            if (res !== null) {
                submitModifyAuthorityRequest(authorityId, res).then((response) => {
                    $.NZ_MsgBox.alert({
                        title: "成功！",
                        content: "修改权限成功",
                        type: "success"
                    });
                    console.log(response)
                    $($($(object).parent().parent()).find("td")[0]).text(res)
                }).catch(function (error) {
                    console.log(error);
                });
                console.log(res)

            }
        }
    });

}

function deleteAuthority(id, object) {
    $.NZ_MsgBox.confirm({
        title: "删除"
        , content: "确认删除？"
        , type: "warning"
        , callback: function (confirm) {
            if (confirm) {
                submitDeleteAuthorityRequest(id, object);
            }
        }
    });

}

/***
 * User Auth
 */
function manageUserRole(object) {
    let id = $(object).attr("data");
    request({
        url: baseUrl + `/admin/api/auth/user/${id}/roles`,
        method: 'get'
    }).then(res => {
        let defaultRoles = res.data.map(item => item.roleName);
        let tagElement = $('#form-tags-4');
        tagElement.attr("data",id);
        tagElement.userId = id;
        tagElement.importTags(defaultRoles.join(","))
        $('#userRoleModal').modal();
    })
}

function saveUserRoles() {
    let tagElement = $('#form-tags-4');
    let tags = tagElement.inputTags();
    let id =  tagElement.attr("data");
    request({
        url: baseUrl + `/admin/api/auth/user/${id}/roles`,
        method: 'post',
        data: tags
    }).then(res => {
        $.NZ_MsgBox.alert({
            title: "成功",
            content: "用户角色修改成功",
            type: "success"
        });
    })
}

function addUser() {

}